<?php

// 采集接口

if( isset($_POST['p_form']) && $_POST['p_form'] == 'send'){
	global $wpdb;

	// 表单变量初始化
	$title		= isset( $_POST['p_title'] ) ? trim($_POST['p_title']) : '';
	$content	= isset( $_POST['p_content'] ) ? $_POST['p_content'] : '';
	$excerpt	= isset( $_POST['p_excerpt'] ) ? $_POST['p_excerpt'] : '';
	$tags		= isset( $_POST['p_tags'] ) ? trim($_POST['p_tags']) : '';
	$category	= isset( $_POST['p_cat'] ) ? (int)$_POST['p_cat'] : 0;
	$name		= isset( $_POST['p_authorname'] ) ? $_POST['p_authorname'] : '';
	$email		= isset( $_POST['p_authoremail'] ) ? trim(htmlspecialchars($_POST['p_authoremail'], ENT_QUOTES)) : '';
	$date		= isset( $_POST['p_date'] ) ? trim(date($_POST['p_date'])) : '';
	$source		= isset( $_POST['p_source'] ) ? trim($_POST['p_source']) : '';

	// 表单项数据验证
	if ( empty($content) ){
		echo '<strong style="color:#F00;">内容不能为空。采集失败！</strong>';
	}

	//插入投稿者信息及宣传语
	$ps = chr(10) . chr(10) . '<p class="ymwz-link">';
	$pe = '</p>';
	$link = '<a href="' . get_bloginfo('siteurl') . '/" target="_blank">' . get_bloginfo('name') . '</a>';
	$text = array(
		$ps . '进入' . $link . '，阅读更多好文章' . $pe,
		$ps . $link . '，原创诗文天地' . $pe,
		$ps . '本文来自：' . $link . $pe,
		$ps . '优美的文字与您分享，' . $link . $pe,
		$ps . $link . '，分享您的故事</p>' . $pe,
	);
	$rt = $text[rand( 0 , count($text) - 1 ) ];
	$p = array(
		'post_title'	=> $title,
		'post_content'	=> $content . $rt,
		'post_excerpt'	=> $excerpt,
		'post_category'	=> array($category),
		'tags_input'	=> $tags,
		'post_date'		=> $date,
		'post_date_gmt'	=> $date,
		'post_author'	=> 2,
		'post_status'	=> 'publish',	//pending审稿状态
	);

	// 将文章插入数据库
	$newPost = wp_insert_post( $p );

	if ( $newPost != 0 ){
		if (!empty( $name )) add_post_meta($newPost, 'name', $name);		//昵称
		if (!empty( $source )) add_post_meta($newPost, 'source', $source);	//来源
		echo '<strong style="color:#090;">采集成功！</strong>';
	}else{
		echo '<strong style="color:#F00;">采集失败！</strong>';
	}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
<form action="<?php echo $_SERVER["REQUEST_URI"]; ?>" method="POST" id="PostForm">
	<input type="hidden" id="p_title" name="p_title" />
	<input type="hidden" id="p_content" name="p_content" />
	<input type="hidden" id="p_excerpt" name="p_excerpt" />
	<input type="hidden" id="p_tags" name="p_tags" />
	<input type="hidden" id="p_authorname" name="p_authorname" />
	<input type="hidden" id="p_authoremail" name="p_authoremail" />
	<input type="hidden" id="p_date" name="p_date" />
	<input type="hidden" id="p_source" name="p_source" />
	<input type="hidden" value="send" name="p_form" />
</form>
</body>
</html>